package ds

import (
	"io"
	"strings"
	"bufio"
	"bytes"
)

type Dictionary map[string]string

func NewDictionary() Dictionary {

	return make(map[string]string)
}

func (d Dictionary) Add(word string) {

	lower := strings.ToLower(word)

	d[lower] = word
}

func (d Dictionary) Canonicalize(word string) (canon string, exists bool) {

	lower := strings.ToLower(word)

	canon, exists = d[lower]

	return
}

func (d Dictionary) Load(in io.Reader) {

	reader := bufio.NewReader(in)

	for line, _, err := reader.ReadLine(); err == nil; line, _, err = reader.ReadLine() {
		d.Add(bytes.NewBuffer(line).String())
	}
}
